package android.money;

import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.money.db.AccountManager;
import android.money.db.DBInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class ReportOption extends Activity {
	private static final int TRANSACTION_LIST_PAGE = Menu.FIRST;
	public static String FROM_DATE = "from";
	public static String TO_DATE = "to";
	private DatePicker formDatePicker;
	private DatePicker toDatePicker;
	private RadioButton allAccountButoon;
	private Cursor accountCursor;
	
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.report_option_page);
		formDatePicker = (DatePicker) findViewById(R.id.fromPicker);
		toDatePicker = (DatePicker) findViewById(R.id.toPicker);
		Spinner cardSpinner = (Spinner) findViewById(R.id.cardSpinner);
		allAccountButoon = (RadioButton)findViewById(R.id.allAccountButton);
		AccountManager accManager = new AccountManager(this);
		accManager.open();
		accountCursor = accManager.fetchAllAccounts();
		startManagingCursor(accountCursor);
		SpinnerAdapter cardAdapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_spinner_dropdown_item, accountCursor,
				new String[] { DBInfo.account_name },
				new int[] { android.R.id.text1 });
		cardSpinner.setAdapter(cardAdapter);

	}
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, TRANSACTION_LIST_PAGE,0, R.string.menu_report).setIcon(android.R.drawable.ic_menu_report_image);
		return true;
	}
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	super.onMenuItemSelected(featureId, item);
		long fromTime = new GregorianCalendar(formDatePicker.getYear(),
				formDatePicker.getMonth(), formDatePicker
						.getDayOfMonth(),0,0,0).getTimeInMillis();
		long toTime = new GregorianCalendar(toDatePicker.getYear(),
				toDatePicker.getMonth(), toDatePicker.getDayOfMonth(),23,59,59)
				.getTimeInMillis();

		Intent i = new Intent(ReportOption.this, TransactionList.class);
		i.putExtra(FROM_DATE, fromTime);
		i.putExtra(TO_DATE, toTime);
		if(!allAccountButoon.isChecked())
			i.putExtra(DBInfo.account_id, accountCursor
				.getInt(accountCursor
						.getColumnIndexOrThrow(DBInfo.account_id)));
		else
			i.putExtra(DBInfo.account_id, -1);
		startActivity(i);
		return true;
	}
	
	
}